using System;
using System.Collections.Generic;
using System.Text;
using Net.Fan.SmallPig.DataAccess;
using System.Collections.ObjectModel;
using System.Data;
using Net.Fan.SmallPig.Common.DBMeta;

namespace Net.Fan.SmallPig.BusinessRules
{
    public class MessageBiz
    {
        const string bugOnlineSender = "BugOnline@BugOnline.net";

        public static string Send(string sender, string receivers, string title, string body)
        {
            char[] chars = new char[] { ';', ',', ' ' };
            string[] receiverArray = receivers.Split(chars, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < receiverArray.Length; i++)
            {
                receiverArray[i] = receiverArray[i].Trim();
            }

            foreach (string receiver in receiverArray)
            {
                if (UserDA.Exist(receiver) == false)
                {
                    return receiver;
                }

            }


            long l = MessageDA.Insert(sender, title, body);

            foreach (string receiver in receiverArray)
            {
                if (MessageDA.InsertReceiver(receiver, l) <= 0)
                {
                    return receiver;
                }
            }

            return null;
        }

        public static bool Notice(Collection<string> receivers, string title, string body)
        {
            bool success = true;
            foreach (string rec in receivers)
            {
                success &= Notice(rec, title, body);
            }
            return success;
        }

        public static bool Notice(string receiver, string title, string body)
        {
            bool success = true;

            if (UserDA.Exist(receiver))
            {
                if (MessageDA.Insert(bugOnlineSender, title, body) <= 0)
                {
                    success = false;
                }
            }
            return success;
        }

        public static bool DeleteMsg(long msgId, string userName)
        {
            int i = MessageDA.SetDeletedFlag(msgId, userName);

            if (MessageDA.GetNoDeleteCount(msgId) == 0)
            {
                MessageDA.TrueDelete(msgId);
            }

            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string GetReceivers(long msgId)
        {
            DataTable dt = MessageDA.GetReceivers(msgId);

            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append(dr[MessageMeta.ReceiverField]);
                sb.Append(' ');
            }
            return sb.ToString();
        }

        public static string DeleteRepeat(string str)
        {
            string[] ss = str.Split(new char[] { ' ', ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<string> ls = new List<string>();

            foreach (string o in ss)
            {
                bool exist = false;
                foreach (string a in ls)
                {
                    if (o == a)
                    {
                        exist = true;
                    }
                }

                if (exist == false)
                {
                    ls.Add(o);
                }
            }

            string temp = string.Empty;
            foreach (string s in ls)
            {
                temp += s;
                temp += ' ';
            }
            return temp;
        }
    }
}
